package com.itboye.ihomebank.home;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.AreaChildAdapter;
import com.itboye.ihomebank.adapter.ChooseAreaAdapter;
import com.itboye.ihomebank.adapter.QuAdapter;
import com.itboye.ihomebank.adapter.QuBean;
import com.itboye.ihomebank.adapter.QuYuAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.HouseListBeanCopy;
import com.itboye.ihomebank.bean.QuYuBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.db.DBManager;
import com.itboye.ihomebank.me.ActivityFaBuHouse;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 私人订制
 * 
 */
public class ActivityShaiXuan extends BaseOtherActivity implements Observer {
	ImageView close_icon;
	TextView add_shap_title_tv;
	// 地点，类型，房租，来源
	RelativeLayout dingzhi_place, dingzhi_type, dingzhi_money, dingzhi_source;
	// 地点，类型，房租，来源
	TextView dingzhi_place_tv, dingzhi_type_tv, dingzhi_money_tv,
			dingzhi_source_tv;
	
	PopupWindow mPopWin;
	/** 弹出框在这个布局的下面 */
	LinearLayout shaixuan_main;
	private SQLiteDatabase database;
	private DBManager dbManager;
	private String cityCode;
	private String city;
	
	HousePresenter housePresenter;
	private LinearLayout layout;
	private RelativeLayout realLayout;
	private ListView cheng_listView;
	private FrameLayout chengqu_layout;
	private ListView qu_listView;
	List<HouseListBeanCopy.HouseItem> arrayArea = new ArrayList<HouseListBeanCopy.HouseItem>();
	List<HouseListBeanCopy.HouseItem.InnerItem> innerItemsArray = new ArrayList<HouseListBeanCopy.HouseItem.InnerItem>();
	
	private String quName;//区
	
	List<Map<String, Object>> list;
	List<Map<String, Object>> list01;
	
	//显示类型
	private String type;
	private String type02;
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.dingzhi_place:// 地点
			showPopupWindow(v);
			break;
		case R.id.dingzhi_type:// 类型
			typePopup(v);
			break;
		case R.id.dingzhi_money:// 房租
			zuJinPopup(v);
			break;
		case R.id.dingzhi_source:// 来源
			laiYuanPopup(v);
			break;
		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		return R.layout.activity_shaixuan;
	}

	@Override
	protected void onInitController() {
		dbManager = new DBManager(this);
		dbManager.openDateBase();
		dbManager.closeDateBase();
		database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/"
				+ DBManager.DB_NAME, null);
		city = SPUtils.get(this, null, SPContants.SELECT_CITY, "") + "";
		if (city != null) {
			cityCode = getCityCode(city);
		}

	}

	@Override
	protected void onInit() {
		add_shap_title_tv.setText("私人订制");
	
	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}
	private String getCityCode(String city) {
		Cursor cursor = null;
		if (database != null)
			cursor = database.query("hat_city", new String[] { "cityID" },
					"city=?", new String[] { city }, null, null, null);
		if (cursor != null) {
			while (cursor.moveToNext()) {
				cityCode = cursor.getString(0);
			}
			cursor.close();
		}
		if (database != null) {
			database.close();
		}
		if (cityCode != null) {
			getListViewData(cityCode);
		}
		return cityCode;
	}
	private void getListViewData(String cityCode) {
		housePresenter = new HousePresenter(this);
		housePresenter.getAreaByCityCode(cityCode);
	}
	/**
	 * 地点
	 */
	private void showPopupWindow(View v) {
		layout = (LinearLayout) LayoutInflater.from(this)
				.inflate(R.layout.popup_chengqu02, null);
		cheng_listView = (ListView) layout.findViewById(R.id.cheng_listView);
		if (arrayArea != null) {
			if (arrayArea.size() > 0) {
				ChooseAreaAdapter cla = new ChooseAreaAdapter(
						this, arrayArea);
				cheng_listView.setAdapter(cla);
				chengqu_layout = (FrameLayout) layout.findViewById(R.id.chengqu_layout);
				qu_listView = (ListView) layout.findViewById(R.id.qu_listView);
				
				chengqu_layout.setVisibility(View.INVISIBLE);// 还占空位，导致点击事件出现bug
				mPopWin = new PopupWindow(layout,
						ViewGroup.LayoutParams.MATCH_PARENT,
						ViewGroup.LayoutParams.WRAP_CONTENT, true);
				mPopWin.setBackgroundDrawable(new BitmapDrawable());
				mPopWin.showAsDropDown(shaixuan_main, 5, 1);
				mPopWin.update();

				cheng_listView.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						if (arrayArea != null) {
							if (arrayArea.size() > 0) {
								innerItemsArray = arrayArea.get(position)
										.getAreaZone();
								if (innerItemsArray.size() > 0) {

									quName=arrayArea.get(position).getName();
									
									chengqu_layout.setVisibility(View.VISIBLE);
									AreaChildAdapter childAdapter = new AreaChildAdapter(ActivityShaiXuan.this,
											innerItemsArray);
									qu_listView.setAdapter(childAdapter);
								} else {
									chengqu_layout.setVisibility(View.INVISIBLE);
								}
							}
						}
					}
				});
				qu_listView.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						String xiaoQu=innerItemsArray.get(position).getName();
						dingzhi_place_tv.setText(quName+" "+xiaoQu); 
						
						mPopWin.dismiss();
					}
				});
			}
		}
	}
	
	
	/**
	 * 类型
	 * @param v
	 */
	private void typePopup(View v){
		
		layout = (LinearLayout) LayoutInflater.from(this)
				.inflate(R.layout.popup_chengqu02, null);
		cheng_listView = (ListView) layout.findViewById(R.id.cheng_listView);
		
		chengqu_layout = (FrameLayout) layout.findViewById(R.id.chengqu_layout);
		qu_listView = (ListView) layout.findViewById(R.id.qu_listView);
		
		list=new ArrayList<Map<String, Object>>();
		Map<String, Object> map01=new HashMap<String, Object>();
		map01.put("aaa", "不限");
		Map<String, Object> map02=new HashMap<String, Object>();
		map02.put("aaa", "整租");
		Map<String, Object> map03=new HashMap<String, Object>();
		map03.put("aaa", "合租");
		list.add(map01);
		list.add(map02);
		list.add(map03);
		
		SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.item_choose_area, 
				new String[] {"aaa"}, new int[] {R.id.name});
		
		cheng_listView.setAdapter(adapter);
		chengqu_layout.setVisibility(View.INVISIBLE);
		
		cheng_listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				if(position==1){
					list01=new ArrayList<Map<String, Object>>();
					Map<String, Object> map01=new HashMap<String, Object>();
					map01.put("bbb", "1居");
					Map<String, Object> map02=new HashMap<String, Object>();
					map02.put("bbb", "2居");
					Map<String, Object> map03=new HashMap<String, Object>();
					map03.put("bbb", "3居");
					Map<String, Object> map04=new HashMap<String, Object>();
					map04.put("bbb", "4+");
					list01.add(map01);
					list01.add(map02);
					list01.add(map03);
					list01.add(map04);
					
					SimpleAdapter adapter01=new SimpleAdapter(MyApplcation.ctx, list01, R.layout.item_choose_area, 
							new String[] {"bbb"}, new int[] {R.id.name});
					chengqu_layout.setVisibility(View.VISIBLE);
					qu_listView.setAdapter(adapter01);
					
				}
				
				if(position==2){
					list01=new ArrayList<Map<String, Object>>();
					Map<String, Object> map01=new HashMap<String, Object>();
					map01.put("bbb", "主卧");
					Map<String, Object> map02=new HashMap<String, Object>();
					map02.put("bbb", "次卧");
					Map<String, Object> map03=new HashMap<String, Object>();
					map03.put("bbb", "单间");
					list01.add(map01);
					list01.add(map02);
					list01.add(map03);
					
					SimpleAdapter adapter01=new SimpleAdapter(MyApplcation.ctx, list01, R.layout.item_choose_area, 
							new String[] {"bbb"}, new int[] {R.id.name});
					chengqu_layout.setVisibility(View.VISIBLE);
					qu_listView.setAdapter(adapter01);
					
				}
				if(position==0){
					chengqu_layout.setVisibility(View.VISIBLE);
					dingzhi_type_tv.setText("不限");
					mPopWin.dismiss();
				}
				type=list.get(position).get("aaa").toString();
			}
		});
		qu_listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				type02=list01.get(position).get("bbb").toString();
				
				dingzhi_type_tv.setText(type+" "+type02);
				
				mPopWin.dismiss();
				
			}
		});
		
		mPopWin = new PopupWindow(layout,
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT, true);
		mPopWin.setBackgroundDrawable(new BitmapDrawable());
		mPopWin.showAsDropDown(shaixuan_main, 5, 1);
		mPopWin.update();
	}
	/**
	 * 租金
	 * @param v
	 */
	private void zuJinPopup(View v){
		realLayout = (RelativeLayout) LayoutInflater.from(this)
				.inflate(R.layout.qiuzu_chengqu, null);
		cheng_listView = (ListView) realLayout.findViewById(R.id.cheng_listView);
		
		list=new ArrayList<Map<String, Object>>();
		Map<String, Object> map01=new HashMap<String, Object>();
		map01.put("aaa", "不限");
		Map<String, Object> map02=new HashMap<String, Object>();
		map02.put("aaa", "500元/月一下");
		Map<String, Object> map03=new HashMap<String, Object>();
		map03.put("aaa", "500-1000元/月");
		Map<String, Object> map04=new HashMap<String, Object>();
		map04.put("aaa", "1000-2000元/月");
		Map<String, Object> map05=new HashMap<String, Object>();
		map05.put("aaa", "2000-3000元/月");
		Map<String, Object> map06=new HashMap<String, Object>();
		map06.put("aaa", "3000-5000元/月");
		Map<String, Object> map07=new HashMap<String, Object>();
		map07.put("aaa", "5000-8000元/月");
		Map<String, Object> map08=new HashMap<String, Object>();
		map08.put("aaa", "8000-10000元/月");
		Map<String, Object> map09=new HashMap<String, Object>();
		map09.put("aaa", "10000元/月以上");
		list.add(map01);
		list.add(map02);
		list.add(map03);
		list.add(map04);
		list.add(map05);
		list.add(map06);
		list.add(map07);
		list.add(map08);
		list.add(map09);
		
		SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.item_choose_area, 
				new String[] {"aaa"}, new int[] {R.id.name});
		
		cheng_listView.setAdapter(adapter);
		
		mPopWin = new PopupWindow(realLayout,
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT, true);
		mPopWin.setBackgroundDrawable(new BitmapDrawable());
		mPopWin.showAsDropDown(shaixuan_main, 5, 1);
		mPopWin.update();
		
		cheng_listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				dingzhi_money_tv.setText(list.get(position).get("aaa").toString());
				mPopWin.dismiss();
			}
		});
		
	}
	
	/**
	 * 来源
	 * @param v
	 */
	private void laiYuanPopup(View v){
		realLayout = (RelativeLayout) LayoutInflater.from(this)
				.inflate(R.layout.qiuzu_chengqu, null);
		cheng_listView = (ListView) realLayout.findViewById(R.id.cheng_listView);
		
		list=new ArrayList<Map<String, Object>>();
		Map<String, Object> map01=new HashMap<String, Object>();
		map01.put("aaa", "不限");
		Map<String, Object> map02=new HashMap<String, Object>();
		map02.put("aaa", "个人");
		Map<String, Object> map03=new HashMap<String, Object>();
		map03.put("aaa", "经纪人");
		list.add(map01);
		list.add(map02);
		list.add(map03);
		
		SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.item_choose_area, 
				new String[] {"aaa"}, new int[] {R.id.name});
		
		cheng_listView.setAdapter(adapter);
		
		mPopWin = new PopupWindow(realLayout,
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT, true);
		mPopWin.setBackgroundDrawable(new BitmapDrawable());
		mPopWin.showAsDropDown(shaixuan_main, 5, 1);
		mPopWin.update();
		
		cheng_listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				dingzhi_source_tv.setText(list.get(position).get("aaa").toString());
				mPopWin.dismiss();
			}
		});
		
	}
	
	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.queryHouseArea_success) {
				HouseListBeanCopy beanCopy = (HouseListBeanCopy) entity
						.getData();
				if (beanCopy != null) {
					arrayArea.addAll(beanCopy.getArea());
				}
			} else if (entity.getEventType() == HousePresenter.queryHouseArea_fail) {
				ByAlert.alert(entity.getData());
			}
		}

	}

	
	
	
}
